local PANEL = {}
PANEL.m_NextRefresh = 0
PANEL.LargeScroll = 0

function PANEL:Init()
	self.m_NextRefresh = RealTime() + 0.01

	self.m_F1Help = EasyLabel(self, "F1: Help Menu", "DefaultSmall")
	self.m_F2Help = EasyLabel(self, "F2: Join/Un-Join Queue", "DefaultSmall")
	self.m_F3Help = EasyLabel(self, "F3: Toggle Radio", "DefaultSmall")
	self.m_F4Help = EasyLabel(self, "F4: Options", "DefaultSmall")

	self.m_AuthorLabel1 = EasyLabel(self, "Gamemode created by "..GAMEMODE.Author, "DefaultSmall")
	self.m_AuthorLabel2 = EasyLabel(self, "Fixed (GMOD13) by "..GAMEMODE.Credits[2][1], "DefaultSmall")
	self.m_AuthorLabel3 = EasyLabel(self, GAMEMODE.Email, "DefaultSmall")
	self.m_AuthorLabel4 = EasyLabel(self, GAMEMODE.Website, "DefaultSmall")

	self.m_HeadingLabel = EasyLabel(self, GAMEMODE.Name, "ScoreboardHead", COLOR_LIMEGREEN)
	self.m_VersionLabel = EasyLabel(self, "v"..GAMEMODE.Version, "ScoreboardSub")
	self.m_ServerNameLabel = EasyLabel(self, GetGlobalString("servername"), "ScoreboardSub")

	local list = vgui.Create("DPanelList", self)
	list:EnableVerticalScrollbar()
	list:EnableHorizontal(false)
	list:SetSpacing(2)
	list:SetPadding(2)
	self.m_LargePanelList = list

	self:InvalidateLayout()
end

function PANEL:PerformLayout()
	self.BaseClass.PerformLayout(self)

	self.m_F1Help:AlignLeft(8)
	self.m_F1Help:AlignTop(8)
	self.m_F2Help:AlignLeft(8)
	self.m_F2Help:MoveBelow(self.m_F1Help, 2)
	self.m_F3Help:AlignLeft(8)
	self.m_F3Help:MoveBelow(self.m_F2Help, 2)
	self.m_F4Help:AlignLeft(8)
	self.m_F4Help:MoveBelow(self.m_F3Help, 2)

	self.m_AuthorLabel1:AlignRight(8)
	self.m_AuthorLabel1:AlignTop(8)
	self.m_AuthorLabel2:AlignRight(8)
	self.m_AuthorLabel2:MoveBelow(self.m_AuthorLabel1, 2)
	self.m_AuthorLabel3:AlignRight(8)
	self.m_AuthorLabel3:MoveBelow(self.m_AuthorLabel2, 2)
	self.m_AuthorLabel4:AlignRight(8)
	self.m_AuthorLabel4:MoveBelow(self.m_AuthorLabel3, 2)

	local wid, hei = self:GetSize()
	local halfwid = wid * 0.5
	local listwid = halfwid - 16
	local y = 8

	self.m_HeadingLabel:AlignTop(20)
	self.m_HeadingLabel:CenterHorizontal()
	self.m_VersionLabel:MoveBelow(self.m_HeadingLabel, 1)
	self.m_VersionLabel:CenterHorizontal()
	self.m_ServerNameLabel:MoveBelow(self.m_VersionLabel, 1)
	self.m_ServerNameLabel:CenterHorizontal()

	self.m_LargePanelList:SetWide(wid - 16)
	self.m_LargePanelList:AlignLeft(8)
	self.m_LargePanelList:MoveBelow(self.m_ServerNameLabel, 8)
	local x, y = self.m_LargePanelList:GetPos()
	self.m_LargePanelList:SetTall(hei - y - 8)
	
	local func = function()
		 self.m_LargePanelList.VBar.SetScroll(self.m_LargePanelList.VBar, self.LargeScroll)
	end
	timer.Simple(0, func)

	local func = function()
		 self.m_LargePanelList.VBar.SetScroll(self.m_LargePanelList.VBar, self.LargeScroll)
	end
	timer.Simple(0, func)
end

function PANEL:Think()
	if self.m_NextRefresh <= RealTime() then
		self.m_NextRefresh = RealTime() + GAMEMODE.HoverGear_RefreshRate
		self:Refresh()
	end
end

function PANEL:Paint()
	local wid, hei = self:GetSize()
	surface.SetDrawColor(10, 10, 10, 180)
	surface.DrawRect(0, 0, wid, hei)
	surface.SetDrawColor(0, 80, 0, 255)
	surface.DrawOutlinedRect(1, 1, wid - 2, hei - 2)
	surface.SetDrawColor(0, 60, 0, 255)
	surface.DrawOutlinedRect(2, 2, wid - 4, hei - 4)
	surface.SetDrawColor(0, 40, 0, 255)
	surface.DrawOutlinedRect(3, 3, wid - 6, hei - 6)
end

function PANEL:RefreshHeading()
	self.m_ServerNameLabel:SetText(GetGlobalString("servername"))
	self.m_ServerNameLabel:SizeToContents()

	self:InvalidateLayout()
end

function PANEL:RefreshLargeList()
	self.LargeScroll = self.m_LargePanelList.VBar:GetScroll()
	self:RefreshList(self.m_LargePanelList, {TEAM_RACERS, TEAM_CROWD})
end

function SortPlayers(a, b)
	if IsValid(a) and IsValid(b) then
		local alap, blap = a:GetLap(), b:GetLap()
		if alap == blap then 
			local acheck, bcheck = a:GetCheckpoint(), b:GetCheckpoint()
			if acheck == bcheck then
				local adist, bdist = a:GetDistance(), b:GetDistance()
				if adist ==  bdist then
					return a:UserID() < b:UserID()
				end
				return adist < bdist
			end
			return acheck > bcheck
		end
		return alap > blap
	else
		return a
	end
end

function PANEL:RefreshList(list, teamid)
	list:Clear(true)

	for i = 1, #teamid, 1 do
		local pls = team.GetPlayers(teamid[i])

		local tall = 40
		--[[if 14 < #pls then
			tall = 24
		elseif 8 < #pls then
			tall = 32
		end]]
		local halftall = tall * 0.5

		local fnt = "Scoreboard"..tall

		table.sort(pls, SortPlayers)
		for i, pl in ipairs(pls) do
			local panel = vgui.Create("DPlayerPanel")
			panel:SetTall(tall)
			list:AddItem(panel)
			panel:SetPlayer(pl)
		end
	end
end

function PANEL:Refresh()
	self:RefreshHeading()
	self:RefreshLargeList()
end

vgui.Register("DScoreBoard", PANEL, "DPanel")